﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace GemCraftLabyrinthHelper
{
    public partial class GemCraftLabSummonHelper : Form
    {
        private Label[] labelsGemGrade;
        private Label[] labelsMonstersPerSummon;
        private Label[] labelsTotalManaGain;
        private Label[] labelsCostPerGem;
        private Label[] labelsProfitPerSummon;

        private List<Label> allLabels;

        public GemCraftLabSummonHelper()
        {
            InitializeComponent();

            labelsGemGrade = new Label[] {
                lblGRD1, lblGRD2, lblGRD3, lblGRD4, lblGRD5, 
                lblGRD6, lblGRD7, lblGRD8, lblGRD9
            };

            labelsMonstersPerSummon = new Label[] {
                lblMPS1, lblMPS2, lblMPS3, lblMPS4, lblMPS5, 
                lblMPS6, lblMPS7, lblMPS8, lblMPS9
            };

            labelsTotalManaGain = new Label[] {
                lblTMG1, lblTMG2, lblTMG3, lblTMG4, lblTMG5, 
                lblTMG6, lblTMG7, lblTMG8, lblTMG9
            };

            labelsCostPerGem = new Label[] {
                lblCPG1, lblCPG2, lblCPG3, lblCPG4, lblCPG5, 
                lblCPG6, lblCPG7, lblCPG8, lblCPG9
            };

            labelsProfitPerSummon = new Label[] {
                lblPPS1, lblPPS2, lblPPS3, lblPPS4, lblPPS5, 
                lblPPS6, lblPPS7, lblPPS8, lblPPS9
            };

            allLabels = new List<Label>();
            allLabels.AddRange(labelsGemGrade);
            allLabels.AddRange(labelsMonstersPerSummon);
            allLabels.AddRange(labelsTotalManaGain);
            allLabels.AddRange(labelsCostPerGem);
            allLabels.AddRange(labelsProfitPerSummon);

            FieldsChanged();
        }

        private void FieldTextChanged(object sender, EventArgs e)
        {
            FieldsChanged();
        }

        private void FieldsChanged()
        {
            int forgeSkill;
            int replenishSkill;
            int waveManaPerMonster;
            int violentSkill;
            double manaPoolMultiplier;

            txtForgeSkill.BackColor = SystemColors.Window;
            txtReplenishSkill.BackColor = SystemColors.Window;
            txtForgeSkill.BackColor = SystemColors.Window;
            txtViolentSkill.BackColor = SystemColors.Window;
            txtManaPoolMult.BackColor = SystemColors.Window;

            if (!int.TryParse(txtForgeSkill.Text, out forgeSkill) || forgeSkill < 0 || forgeSkill > 30)
            {
                lblErrorMessage.Text = "Invalid Forge Skill value.\nEnter an integer between (inclusive) 0 and 30";
                txtForgeSkill.BackColor = Color.MistyRose;
                return;
            }

            if (!int.TryParse(txtReplenishSkill.Text, out replenishSkill) || replenishSkill < 0)
            {
                lblErrorMessage.Text = "Invalid Replenish Skill value.\nEnter an integer greater or equal to 0";
                txtReplenishSkill.BackColor = Color.MistyRose;
                return;
            }

            if (!int.TryParse(txtWaveMana.Text, out waveManaPerMonster) || waveManaPerMonster <= 0)
            {
                lblErrorMessage.Text = "Invalid Replenish Skill value.\nEnter an integer greater than 0";
                txtWaveMana.BackColor = Color.MistyRose;
                return;
            }

            if (!int.TryParse(txtViolentSkill.Text, out violentSkill) || violentSkill < 0 || violentSkill > 15)
            {
                lblErrorMessage.Text = "Invalid Violent Explosions Skill value.\nEnter an integer between (inclusive) 0 and 15";
                txtViolentSkill.BackColor = Color.MistyRose;
                return;
            }

            string manaTemp = txtManaPoolMult.Text.Replace(",", ".");

            if (!double.TryParse(manaTemp, NumberStyles.AllowDecimalPoint, CultureInfo.CreateSpecificCulture("en-US"), out manaPoolMultiplier) || manaPoolMultiplier < 1 || Math.Round(manaPoolMultiplier * 100) % 5 != 0)
            {
                lblErrorMessage.Text = "Invalid Mana Pool Multiplier value.\nEnter a decimal value greater than or equal to 1 in 0.05 steps";
                txtManaPoolMult.BackColor = Color.MistyRose;
                return;
            }

            lblErrorMessage.Text = "";

            FillScreen(Rule.Calculate(forgeSkill, replenishSkill, waveManaPerMonster, violentSkill, manaPoolMultiplier));
        }

        private void FillScreen(CalculatedData data)
        {
            Font regularFont = new Font(allLabels[0].Font, FontStyle.Regular);
            Font boldFont = new Font(allLabels[0].Font, FontStyle.Bold);

            foreach (var item in allLabels)
            {
                item.ForeColor = Color.Black;
                item.Font = regularFont;
            }

            foreach (var item in labelsTotalManaGain)
            {
                item.ForeColor = Color.Navy;
            }

            int maxProfitIndex = 0;
            double maxProfitValue = 0;

            for (int i = 0; i < data.CostPerGem.Length; i++)
            {
                labelsMonstersPerSummon[i].Text = data.MonstersPerSummon[i].ToString();
                labelsTotalManaGain[i].Text = data.TotalManaGain[i].ToString("F0");
                labelsCostPerGem[i].Text = data.CostPerGem[i].ToString();
                labelsProfitPerSummon[i].Text = data.ProfitPerSummon[i].ToString("F0");

                if (data.ProfitPerSummon[i] > maxProfitValue)
                {
                    maxProfitIndex = i;
                    maxProfitValue = data.ProfitPerSummon[i];
                }

                if (data.ProfitPerSummon[i] >= 0)
                {
                    labelsProfitPerSummon[i].ForeColor = Color.Green;
                }
                else
                {
                    labelsProfitPerSummon[i].ForeColor = Color.Red;
                }
            }

            labelsGemGrade[maxProfitIndex].ForeColor = Color.Goldenrod;
            labelsGemGrade[maxProfitIndex].Font = boldFont;
            labelsMonstersPerSummon[maxProfitIndex].ForeColor = Color.Goldenrod;
            labelsMonstersPerSummon[maxProfitIndex].Font = boldFont;
            labelsTotalManaGain[maxProfitIndex].ForeColor = Color.Goldenrod;
            labelsTotalManaGain[maxProfitIndex].Font = boldFont;
            labelsCostPerGem[maxProfitIndex].ForeColor = Color.Goldenrod;
            labelsCostPerGem[maxProfitIndex].Font = boldFont;
            labelsProfitPerSummon[maxProfitIndex].ForeColor = Color.Goldenrod;
            labelsProfitPerSummon[maxProfitIndex].Font = boldFont;
        }
    }
}
